<?php
/**
 * Nweb Framework
 *
 * This script is protected by copyright. It's use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.framework.uri
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     3.0 $Id$
 * @link        http://code.google.com/p/nweb-framework/
 * @link        http://framework.nweb.pl
 */

namespace pl\nweb\framework\uri;

/**
 * URI
 *
 * @category    source
 * @package     pl.nweb.framework.uri
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @version     3.0 $Id$
 */
class Builder
{
    /**
     * Formatowanie adresu url
     *
     * @param string $url adres url
     * @param array $params parametry adresu url (query)
     * @return string
     */
    public static function create ($url = '', array $params = array())
    {
        $parts = parse_url($url);

        $url = '';
        if(isset($parts['scheme'])) {
            $url .= $parts['scheme'] . '://';
        } else {
            $url .= 'scheme://'; // todo
        }

        if(isset($parts['host'])) {
            $url .= $parts['host'];
        } else {
            $url .= 'get hosts'; // todo
        }

        if(!isset($parts['path'])) {
            $url .= '/';
        } else {
            $url .= $parts['path'];
        }

        if(isset($parts['query']) || !empty($params)) {
            $url .= '?';
            $amp  = null;
            if(isset($parts['query'])) {
                $url .= $parts['query'];
                $amp  = '&';
            }
            if(!empty($params)) {
                $url .= $amp . http_build_query($params);
            }
        }
        return $url;
    }
}